package com.iot08.api.service;

import com.iot08.api.config.redis.RedisFilterService;
import com.iot08.api.constant.Constant;
import com.iot08.api.vo.request.SystemLimitAddReqVo;
import com.iot08.api.vo.request.SystemLimitEditReqVo;
import com.iot08.common.entity.app.SystemLimit;
import com.iot08.common.service.base.BaseAdminService;
import com.iot08.common.utils.response.ResponseMap;
import lombok.extern.slf4j.Slf4j;
import org.bson.types.ObjectId;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @author cwj
 * @description
 * @date 2025/1/8 16:25
 **/
@Slf4j
@Service
public class SystemLimitService extends BaseAdminService<SystemLimit> {

    @Resource
    private RedisFilterService redisFilterService;

    /**
     * @description 查询系统限制条件
     * @author cwj
     * @param []
     * @return com.iot08.common.entity.app.SystemLimit
     * @date @time 2025/1/8 16:31
     **/
    public SystemLimit querySystemLimit(){
        List<SystemLimit> systemLimits = mongoTemplate.findAll(getTClass());
        if(CollectionUtils.isEmpty(systemLimits)){
            return null;
        }
        return systemLimits.get(0);
    }


    /**
     * @description 新增系统限制
     * @author cwj
     * @param [vo]
     * @return com.iot08.common.utils.response.ResponseMap
     * @date @time 2025/1/9 14:29
     **/
    public ResponseMap addSystemLimit(SystemLimitAddReqVo vo) {
        SystemLimit systemLimit = querySystemLimit();
        if(ObjectUtils.isEmpty(systemLimit)){
            systemLimit = new SystemLimit();
            systemLimit.setDeviceSendVoiceTimesUpLimit(vo.getDeviceSendVoiceTimesUpLimit());
            systemLimit.setUserSendVoiceTimesUpLimit(vo.getUserSendVoiceTimesUpLimit());
            systemLimit.setCreateTime(new Date());
            systemLimit.setUpdateTime(new Date());
            this.save(systemLimit);
            //设置缓存
            redisFilterService.set(Constant.DEVICE_SEND_VOICE_TIMES_UP_LIMIT_REDIS_KEY,vo.getDeviceSendVoiceTimesUpLimit().toString());
            redisFilterService.set(Constant.USER_SEND_VOICE_TIMES_UP_LIMIT_REDIS_KEY,vo.getUserSendVoiceTimesUpLimit().toString());
        } else {
            ResponseMap.fail("系统限制数据已经存在，无法再新增");
        }
        return ResponseMap.success();
    }


    /**
     * @description 修改系统限制
     * @author cwj
     * @param [vo]
     * @return com.iot08.common.utils.response.ResponseMap
     * @date @time 2025/1/9 14:29
     **/
    public ResponseMap editSystemLimit(SystemLimitEditReqVo vo){
        Update update = new Update();
        update.set("deviceSendVoiceTimesUpLimit",vo.getDeviceSendVoiceTimesUpLimit());
        update.set("userSendVoiceTimesUpLimit",vo.getUserSendVoiceTimesUpLimit());
        update.set("updateTime",new Date());
        this.updateById(new ObjectId(vo.getSid()),update);
        redisFilterService.set(Constant.DEVICE_SEND_VOICE_TIMES_UP_LIMIT_REDIS_KEY,vo.getDeviceSendVoiceTimesUpLimit().toString());
        redisFilterService.set(Constant.USER_SEND_VOICE_TIMES_UP_LIMIT_REDIS_KEY,vo.getUserSendVoiceTimesUpLimit().toString());
        return ResponseMap.success();
    }

}
